package com.bootdo.system.service.impl;

import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.dao.ReportImgDao;
import com.bootdo.system.domain.ReportImgDO;
import com.bootdo.system.service.ReportImgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class ReportImgServiceImpl implements ReportImgService {
	private Logger logger = LoggerFactory.getLogger(ReportImgServiceImpl.class);

	@Autowired
	private ReportImgDao reportImgDao;

	@Value("${bootdo.uploadPath}")
	private String uploadPath;
	
	@Override
	public ReportImgDO get(String id){
		return reportImgDao.get(id);
	}
	
	@Override
	public List<ReportImgDO> list(Map<String, Object> map){
		return reportImgDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return reportImgDao.count(map);
	}
	
	@Override
	public int save(MultipartFile[] files) throws Exception{
		for(int i=0;i<files.length; i++){
			String id = UUID.randomUUID().toString().replaceAll("-","");
			String imgUrl = FileUtil.upload(files[i], uploadPath, id);

			/**
			 * 1、针对无需区分【规格】、【有效期】的质检报告，
			 * 只有1张质检报告 图片命名（商品名称#批号）；
			 * 有多张质检报告  图片命名（商品名称#批号#1，商品名称#批号#2，依次类推）；
			 * 2、针对需要区分规格【规格】、【有效期】的质检报告，
			 * 只有1张质检报告 图片命名（商品名称#批号#规格#有效期）；
			 * 有多张质检报告  图片命名（商品名称#批号#1#规格#有效期，商品名称#批号#2#规格#有效期，依次类推）；
			 */
			String fileName = files[i].getOriginalFilename();
			fileName = fileName.substring(0, fileName.lastIndexOf("."));
			if(fileName.split("#").length != 2 && fileName.split("#").length != 3
			&& fileName.split("#").length != 4 && fileName.split("#").length != 5){
				logger.error("质检报告上传失败!fileName:" + files[i].getOriginalFilename());
				throw new Exception("质检报告名字不符合格式!" + files[i].getOriginalFilename());
			}
			String[] arry = fileName.split("#");
			String imgOrder = "0";
			if(arry.length == 3){
				imgOrder =  arry[2];
			}

			String productGuige = "";
			String validDate = "";
			if(arry.length == 4){
				productGuige = arry[2];
				validDate = arry[3];
			}

			if(arry.length == 5){
				imgOrder = arry[2];
				productGuige = arry[3];
				validDate = arry[4];
			}
			String productName = arry[0];
			String batchNumber = arry[1];
			//查询是否上传过 该产品 批号 次序 的质检报告
			List<ReportImgDO> reportImg = reportImgDao.getImgInfo(productName, batchNumber, imgOrder, productGuige, validDate);
			if(!reportImg.isEmpty()){
				//上传过了先删除
				reportImgDao.remove(reportImg.get(0).getId());
			}
			ReportImgDO reportImgDO = new ReportImgDO();
			reportImgDO.setId(id);
			reportImgDO.setProductName(productName);
			reportImgDO.setBatchNumber(batchNumber);
			reportImgDO.setImgOrder(imgOrder);
			reportImgDO.setProductGuige(productGuige);
			reportImgDO.setValidDate(validDate);
			reportImgDO.setImgUrl("/files" + imgUrl);
			reportImgDO.setCreateTime(new Date());
			reportImgDO.setCreateUser(ShiroUtils.getUserId().toString());
			reportImgDO.setIsDelete(0);
			reportImgDao.save(reportImgDO);
		}
		return 1;
	}
	
	@Override
	public int update(ReportImgDO reportImg){
		return reportImgDao.update(reportImg);
	}
	
	@Override
	public int remove(String id){
		return reportImgDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return reportImgDao.batchRemove(ids);
	}

	@Override
	public List<ReportImgDO> getImgInfo(String productName, String batchNumber, String imgOrder, String productGuige, String validDate) {
		return reportImgDao.getImgInfo(productName, batchNumber, imgOrder, productGuige, validDate);
	}
}
